﻿@using WebAPP_LIULI.Models;
@{
    HalfWarehousing m = Model;
    if (m == null)
    {
        m = new HalfWarehousing();
    }
    User user = (User)Session["User"];
}

<!-- Page-Title -->
<div class="row">
    <div class="col-sm-12">

        <h4 class="page-title">半成品交检</h4>
    </div>
</div>
<!-- end page title end breadcrumb -->


<div class="row">

    <div class="col-12">
        <div class="card-box">
            <form id="FormBody" class="form-horizontal" role="form" >
                <table class="table">
                    <thead>
                        <tr>
                            @*<th></th>*@
                            <th width="30%">产品名称</th>
                            <th width="40%">交检数量/吨</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            @*<th scope="row">@(ViewBag.products.IndexOf(c)+1)</th>*@
                            <td>半成品<input hidden="hidden" name="ProductName" value="半成品"></td>
                            <td><input class="form-control" type="number" name="InspectionCount"><input hidden="hidden" type="text" name="InspectionUserName" value="@if (string.IsNullOrEmpty(m.InspectionUserName)) {@user.UserName  } else { @m.InspectionUserName; }"></td>
                        </tr>
                    </tbody>
                </table>

                <div class="form-group row">
                    <label class="col-md-2 col-form-label">备注</label>
                    <div class="col-md-10">
                        <textarea class="form-control" name="Remarks" id="Remarks" rows="2"></textarea>
                    </div>
                </div>

                <dl class="row p-t-10">
                    <dt class="col-6">交货人</dt>
                    <dd class="col-6">@if (string.IsNullOrEmpty(m.InspectionUserName)) { @user.UserName } else { @m.InspectionUserName; }</dd>
                </dl>

                <button type="button" class="btn btn-block btn-xs btn-purple waves-effect waves-light mt-2" id="formsubmit">提交</button>
            </form>


        </div>
    </div><!-- end col -->

    <script>
        $(document).ready(function () {
            $('#formsubmit').click(function () {
                swal({
                    title: '确定提交么？',
                    text: "请确定好填写内容正确无误后提交！",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#4fa7f3',
                    cancelButtonColor: '#d57171',
                    confirmButtonText: '提交',
                    cancelButtonText: '取消'
                }).then(function () {
                    var warehousings = new Array();
                    var Remarks = $('#Remarks').val();
                    $.each($('tbody').children('tr'), function (i, val) {
                        var ProductName = $($(val).find('input[name="ProductName"]')[0]).val();
                        var InspectionCount = $($(val).find('input[name="InspectionCount"]')[0]).val();
                        var InspectionUserName = $($(val).find('input[name="InspectionUserName"]')[0]).val();
                        
                        var postdata =
                        {
                            ProductName: ProductName,
                            InspectionCount: InspectionCount,
                            InspectionUserName: InspectionUserName,
                            Remarks:Remarks
                        };

                        warehousings.push(postdata);
                    });

                    $.post('/Home/HalfWarehousingInspection', { warehousings }, function (data) {
                        var jsonObject = JSON.parse(data);
                        if (jsonObject.success == true) {
                            swal(
                                '提交成功！',
                                '您已成功提交！',
                                'success'
                            )
                            window.location = '/Home/Index';
                        }
                        else {
                            swal(
                                '提交失败！',
                                '可能因为网络原因提交失败，请重试！',
                                'error'
                            )
                        }

                    });

                    
                    
                }, function (dismiss) {
                    // dismiss can be 'cancel', 'overlay',
                    // 'close', and 'timer'
                    if (dismiss === 'cancel') {
                        swal(
                            '已取消',
                            '请核对信息后再次提交！',
                            'error'
                        )
                    }
                })
            });
        });

        $('#InspectionCount').change(function () {
            $('#Ton').html($('#InspectionCount').val() * 1.5 + '吨');
        });

        $('input[name="InspectionCount"').change(function (val) {
            var countInput = val.currentTarget;
            var count = $(countInput).val();
            var inputTd =  countInput.parentNode;
            var tr =  inputTd.parentNode;
            var tonTd = tr.children[2];
            $(tonTd).html(count * 1.5 + '吨');
        });
    </script>

</div>
<!-- end row -->